<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2019/1/16
 * Time: 14:53
 */
namespace APP\HttpController\Lib\Redis;
use EasySwoole\Core\AbstractInterface\Singleton;

class Redis{
    use Singleton;
    public $redis = "";
    private function __construct()
    {
        if (!extension_loaded('redis')){
            throw new \Exception('redis.so文件不存在');
        }
        try{
            $redisConfig = \Yaconf::get('redis');
            $this->redis = new \Redis();
            $result = $this->redis->connect($redisConfig['host'],$redisConfig['port'],$redisConfig['time_out']);
        }catch (\Exception $e){
            throw new \Exception("redis服务异常");
        }

        if ($result === false){
            throw new \Exception("redis 链接失败");
        }
    }

    /**
     * @param $key
     * @return string
     */
    public function get($key){

        if (empty($key)){
            return '';
        }
      return  $this->redis->get($key);
    }

    /**
     * @param $key
     * @return string
     */
    public function lPop($key){
        if (empty($key)){
            return '';
        }
        return  $this->redis->lPop($key);
    }

}